package csc.fresher.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import csc.fresher.dao.UserDAOImpl;
import csc.fresher.domain.User;

/**
 * Servlet implementation class EditUserServlet
 */

public class EditUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EditUserServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("inputName");
		String password = request.getParameter("inputPassword");
		String repeatPassword = request.getParameter("inputNewPassword");
		String page = request.getParameter("inputPage");
		UserDAOImpl dao = new UserDAOImpl();
		User user = dao.getUser(username);
		if (password.length() < 8 || repeatPassword.length() < 8) {
			request.setAttribute("messageEdit",
					"Sorry ! Password must be at least 8 character");
			if (page.equals("profile.jsp")) {
				request.setAttribute("userprofile", user);
				request.getRequestDispatcher("profile.jsp").forward(request,
						response);
			} else {
				request.setAttribute("listUser", dao.getListUser());
				request.getRequestDispatcher("listuser.jsp").forward(request,
						response);
			}
		} else {
			if (!password.equals(repeatPassword)) {
				request.setAttribute("messageEdit",
						"Sorry ! Please insert the same 2 password");
				if (page.equals("profile.jsp")) {
					request.setAttribute("userprofile", user);
					request.getRequestDispatcher("profile.jsp").forward(
							request, response);
				} else {
					request.setAttribute("listUser", dao.getListUser());
					request.getRequestDispatcher("listuser.jsp").forward(
							request, response);
				}
			} else {
				if (dao.editUser(username, password)) {
					User usernew = dao.getUser(username);
					request.setAttribute("messageEdit",
							"Congratulation ! You update success");
					usernew.setModifiedDate(new Date());
					if (page.equals("profile.jsp")) {
						request.setAttribute("userprofile", usernew);
						request.getRequestDispatcher("profile.jsp").forward(
								request, response);
					} else {
						System.out.println(true);
						request.setAttribute("listUser", dao.getListUser());
						request.getRequestDispatcher("listuser.jsp").forward(
								request, response);
					}
				}
			}
		}
	}

}
